package com.zxbit.uangstore.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.zxbit.uangstore.R;
import com.zxbit.uangstore.utils.PXUtils;

public class NormalDialog extends Dialog implements View.OnClickListener {

    private View rootView;
    private TextView tv_tile;
    private TextView tv_content;
    private Button left_button;
    private Button right_button;
    private NormalClickListener mNormalClickListener;

    public NormalDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public NormalDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected NormalDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_normal_layout, null);
        tv_tile = rootView.findViewById(R.id.tv_tile);
        tv_content = rootView.findViewById(R.id.tv_content);
        left_button = rootView.findViewById(R.id.left_button);
        right_button = rootView.findViewById(R.id.right_button);
        setContentView(rootView);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setLayout(PXUtils.dp2px(getContext(), 260), WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏

        left_button.setOnClickListener(this);
        right_button.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
    }


    public NormalDialog setTitle(String title) {
        if (tv_tile != null) {
            tv_tile.setText(title);
        }
        return this;
    }


    public NormalDialog setContent(String contentStr) {
        if (tv_content != null) {
            tv_content.setText(contentStr);
        }
        return this;
    }


    public NormalDialog setLeftBtnText(String leftText) {
        if (left_button != null) {
            left_button.setText(leftText);
        }
        return this;
    }


    public NormalDialog setRightBtnText(String rightText) {
        if (right_button != null) {
            right_button.setText(rightText);
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onLeftClick();
                }
                break;
            case R.id.right_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onRightClick();
                }

                break;
        }
    }


    public NormalDialog setNormalClickListener(NormalClickListener listener) {
        mNormalClickListener = listener;
        return this;
    }

    public interface NormalClickListener {
        void onLeftClick();

        void onRightClick();
    }
}
